package com.campus.controller;

import com.campus.common.ResponseResult;
import com.campus.dto.ActivityCreateDto;
import com.campus.dto.ActivityQueryDto;
import com.campus.exception.BusinessException;
import com.campus.model.Activity;
import com.campus.service.ActivityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc: 活动控制器类，用于处理活动相关的HTTP请求
 * @author: hjm
 * @date: 2024/11/12 14.51
 */
@RestController
@RequestMapping("/activity")
public class ActivityController {

    private static final Logger log = LoggerFactory.getLogger(ActivityController.class);

    @Autowired
    private ActivityService activityService;

    /**
     * 创建活动
     *
     * @param activityCreateDto 活动创建DTO对象
     * @return 包含创建活动信息的ResponseResult
     */
    @PostMapping("/create")
    public ResponseResult<Activity> createActivity(@RequestBody ActivityCreateDto activityCreateDto) {
        log.info("接收到创建活动请求，活动名称：{}", activityCreateDto.getName());
        try {
            Activity activity = activityService.createActivity(activityCreateDto);
            return ResponseResult.ok(activity, "活动创建成功");
        } catch (BusinessException e) {
            log.error("创建活动时发生错误：{}", e.getMessage());
            return ResponseResult.error(HttpStatus.BAD_REQUEST, e.getMessage());
        } catch (Exception e) {
            log.error("创建活动时发生错误：{}", e.getMessage());
            return ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "创建活动时发生错误");
        }
    }

    /**
     * 更新活动
     *
     * @param id 活动ID
     * @param activityCreateDto 活动创建DTO对象
     * @return 包含更新活动信息的ResponseResult
     */
    @PutMapping("/{id}/update")
    public ResponseResult<Activity> updateActivity(
            @PathVariable("id") int id,
            @RequestBody ActivityCreateDto activityCreateDto) {
        log.info("接收到更新活动请求，活动ID：{}", id);
        try {
            Activity updatedActivity = activityService.updateActivity(id, activityCreateDto);
            return ResponseResult.ok(updatedActivity, "活动更新成功");
        } catch (BusinessException e) {
            log.error("更新活动时发生错误：{}", e.getMessage());
            return ResponseResult.error(HttpStatus.BAD_REQUEST, e.getMessage());
        } catch (Exception e) {
            log.error("更新活动时发生错误：{}", e.getMessage());
            return ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "更新活动时发生错误");
        }
    }

    /**
     * 发布活动
     *
     * @param id 活动ID
     * @return 包含发布活动结果信息的ResponseResult
     */
    @PostMapping("/{id}/publish")
    public ResponseResult<String> publishActivity(@PathVariable("id") int id) {
        log.info("接收到发布活动请求，活动ID：{}", id);
        try {
            boolean success = activityService.publishActivity(id);
            if (success) {
                return ResponseResult.ok("活动已成功发布");
            } else {
                return ResponseResult.error(HttpStatus.BAD_REQUEST, "活动发布失败");
            }
        } catch (BusinessException e) {
            log.error("发布活动时发生错误：{}", e.getMessage());
            return ResponseResult.error(HttpStatus.BAD_REQUEST, e.getMessage());
        } catch (Exception e) {
            log.error("发布活动时发生错误：{}", e.getMessage());
            return ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "发布活动时发生错误");
        }
    }

    /**
     * 查询活动列表
     *
     * @param status 活动状态
     * @param nameKeyword 活动名称关键词
     * @param sortBy 排序字段
     * @return 包含活动列表的ResponseResult
     */
    @GetMapping("/query")
    public ResponseResult<List<Activity>> queryActivities(
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "nameKeyword", required = false) String nameKeyword,
            @RequestParam(value = "sortBy", defaultValue = "created_time") String sortBy) {
        log.info("接收到查询活动列表请求，状态：{}，关键词：{}，排序字段：{}", status, nameKeyword, sortBy);
        try {
            ActivityQueryDto queryDto = new ActivityQueryDto();
            queryDto.setStatus(status);
            queryDto.setNameKeyword(nameKeyword);
            queryDto.setSortBy(sortBy);

            List<Activity> activities = activityService.queryActivities(queryDto);
            return ResponseResult.ok(activities, "查询成功");
        } catch (BusinessException e) {
            log.error("查询活动列表时发生错误：{}", e.getMessage());
            return ResponseResult.error(HttpStatus.BAD_REQUEST, e.getMessage());
        } catch (Exception e) {
            log.error("查询活动列表时发生错误：{}", e.getMessage());
            return ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "查询活动列表时发生错误");
        }
    }
}